/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javafx179labs.nez.hr.dao.file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author SSheypak
 */
//Visible only in package
class DataReaderHelper {
    private List<String> lines = new ArrayList<String>();
    
    public DataReaderHelper(String pathToFile){
        BufferedReader bufferedReader = null;
        try{
            bufferedReader = new BufferedReader(new FileReader(pathToFile));
            String line = null;
            while( (line = bufferedReader.readLine()) !=null ){
                lines.add(line);
            }
        }catch(Exception e){
            System.out.println("Error while reading file["+pathToFile+"]. " + e.getMessage());
            e.printStackTrace();
        }finally{ //always release resources. They can't be managed by Garbage collector.
            closeReader(bufferedReader);
        }
    }

    public List<String> getLines(){
        return lines;
    }
    
    private void closeReader(Reader reader){
        if(reader!=null){
            try {
                reader.close();
            } catch (Exception e) {
                System.out.println("Error while closin reader. " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
